<?php
include_once dirname(__FILE__).'/../RDBConfig.php';
include_once dirname(__FILE__).'/../../interface/ProjectFolderDAOInterface.php';
include_once dirname(__FILE__).'/../../object/ProjectFolder.php';

class ProjectFolderDAO implements ProjectFolderDAOInterface {
	
	public static $TABLE_NAME = 'PROJECT_FOLDER';
	public static $COL_FOLDER_PK = 'FOLDER_PK';
	public static $COL_PROJECT_PK = 'PROJECT_PK';
	public static $COL_FOLDER_NAME = 'FOLDER_NAME';
	
	public function __construct() {
	
	}
	
	/**
	 * Them mot dong vao 'PROJECT_FOLDER'.
	 * Tra ve so dong da them
	 * @see ProjectFolderDAOInterface::insert()
	 * @param ProjectFolder $folder
	 * @return int
	 */
	public function insert($folder) {
		$table = self::$TABLE_NAME;
		$colFolderPK = self::$COL_FOLDER_PK;
		$colProjectPK = self::$COL_PROJECT_PK;
		$colFolderName = self::$COL_FOLDER_NAME;
		
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		// tao cau truy van (luu y chu END; cuoi khong co dau cach phia truoc!)
		$statement = <<<END
        INSERT INTO $table
        	($colProjectPK, $colFolderName)
                VALUES (:project_pk, :folder_name)
END;
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$data = array( // chuan bi du lieu
				':project_pk' => $folder->getProjectPK(),
				':folder_name'=> $folder->getFolderName()
		);
		
		try {
			$conn->beginTransaction(); // bo che do tu dong commit
		
			$sql->execute($data); // do du lieu vao cau truy van
		
			$conn->commit(); // commit de cap nhat thay doi vao CSDL
		
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
			$conn->rollBack();
		}
		
		$conn = null; // free memory
		return $sql->rowCount(); // tra ve so dong da them
	}
	
	/**
	 * Xoa mot dong co PK tuong ung.
	 * Tra ve so dong da xoa.
	 * @see ProjectFolderDAOInterface::delete()
	 * @param ProjectFolder $folder
	 * @return int
	 */
	public function delete($folder) {
		
	}
	
	public function update($old, $new) {
		
	}
	
	/**
	 * Tra ve cac Folder cua Project co PK tuong ung.
	 * @see ProjectFolderDAOInterface::getFolderByProject()
	 * @param Project $project
	 * @return ProjectFolder (array)
	 */
	public function getFolderByProject($project) {
		$table = self::$TABLE_NAME;
		$colFolderPK = self::$COL_FOLDER_PK;
		$colProjectPK = self::$COL_PROJECT_PK;
		$colFolderName = self::$COL_FOLDER_NAME;
		
		$tableProject = ProjectDAO::$TABLE_NAME;
		
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		// tao cau truy van (luu y chu END; cuoi khong co dau cach phia truoc!)
		$statement = <<<END
        SELECT a.$colFolderPK, a.$colProjectPK, a.$colFolderName
        FROM $table a, $tableProject b
        WHERE 	a.$colProjectPK = b.$colProjectPK
        	AND	b.$colProjectPK = :project_pk
END;
				
			$data = array( // chuan bi du lieu
					':project_pk' => $project->getProjectPK()
			);
		
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$folders = array();
		try {
			$sql->execute($data); // do du lieu vao cau truy van
			$sql->setFetchMode(PDO::FETCH_ASSOC);
			foreach ($sql->fetchAll() as $fol) {
				$folders[] = new ProjectFolder($fol["$colFolderPK"],
						$fol["$colProjectPK"], $fol["$colFolderName"]);
			}
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
		}
		$conn = null; // free memory
		
		return $folders;
	}
	
}
?>
